﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MedicalDB.Export;

namespace MedicalDB
{
    public partial class FormDBMedical : Form
    {
        private static FormDBMedical instance;

        public string limba;
        public static string ServerIP;
        public static string ServerPort;
        public static string FTPIp { get; set; }
        public static string FTPUser { get; set; }
        public static string FTPParola { get; set; }
        public static string FTPPort { get; set; }

        public Control control;
        private AlertListener.AlertListener alertListener;

        public static string nume = string.Empty;
        public static string prenume = string.Empty;
        public static string CNP = string.Empty;

        public FormDBMedical(string limba, string ServerIP, string ServerPort, string FTPIp, string FTPUser, string FTPParola, string FTPPort)
        {
            instance = this;
            InitializeComponent();

            if (CNP.Equals(string.Empty))
            {
                lbPacient.Visible = false;
                adaugareInformatiiPacientToolStripMenuItem.Enabled = false;
                instance.imaginiMedicaleToolStripMenuItem.Enabled = false;
                instance.VizualizareSenzoritoolStripMenu.Enabled = false;
                instance.generarePDFToolStripMenuItem.Enabled = false;
            }

            Rename(limba);
            this.limba = limba;

            FormDBMedical.ServerIP = ServerIP;
            FormDBMedical.ServerPort = ServerPort;
            FormDBMedical.FTPIp = FTPIp;
            FormDBMedical.FTPUser = FTPUser;
            FormDBMedical.FTPParola = FTPParola;
            FormDBMedical.FTPPort = FTPPort;

            alertListener = new AlertListener.AlertListener(ServerIP, ServerPort);
            alertListener.Start();
        }

        public void rename()
        {
            Rename(limba);
        }


        public static void ActualizarePacientNume()
        {
            if (!CNP.Equals(string.Empty))
            {
                instance.lbPacient.Text = "Pacient selectat: " + nume +
                    " " + prenume +
                    " CNP:" + CNP;
                instance.lbPacient.Visible = true;
                instance.adaugareInformatiiPacientToolStripMenuItem.Enabled = true;
                instance.imaginiMedicaleToolStripMenuItem.Enabled = true;
                instance.VizualizareSenzoritoolStripMenu.Enabled = true;
                instance.generarePDFToolStripMenuItem.Enabled = true;
            }
        }

        public void resetGUI()
        {
            this.Controls.Remove(control);
        }

        private void addPacientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugarePacient adaugaPacient = new AdaugarePacient(limba);
            adaugaPacient.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaPacient.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaPacient.Dock = DockStyle.Fill;
            control = adaugaPacient;
            this.Controls.Add(adaugaPacient);

        }


        private void evaluareSistemeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareEvaluareSisteme adaugaEvaluareSisteme = new AdaugareEvaluareSisteme(limba, CNP);
            adaugaEvaluareSisteme.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaEvaluareSisteme.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaEvaluareSisteme.Dock = DockStyle.Fill;
            control = adaugaEvaluareSisteme;
            this.Controls.Add(adaugaEvaluareSisteme);
        }

        private void evaluareSocialaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareEvaluareSociala adaugaEvaluareSociala = new AdaugareEvaluareSociala(limba, CNP);
            adaugaEvaluareSociala.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaEvaluareSociala.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaEvaluareSociala.Dock = DockStyle.Fill;
            control = adaugaEvaluareSociala;
            this.Controls.Add(adaugaEvaluareSociala);
        }

        private void fisaExternareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareFisaExternare adaugaFisaExternare = new AdaugareFisaExternare(limba, CNP);
            adaugaFisaExternare.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaFisaExternare.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaFisaExternare.Dock = DockStyle.Fill;
            control = adaugaFisaExternare;
            this.Controls.Add(adaugaFisaExternare);
        }

        private void functiiVitaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareFunctiiVitale adaugaFunctiiVitale = new AdaugareFunctiiVitale(limba, CNP);
            adaugaFunctiiVitale.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaFunctiiVitale.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaFunctiiVitale.Dock = DockStyle.Fill;
            control = adaugaFunctiiVitale;
            this.Controls.Add(adaugaFunctiiVitale);
        }

        private void vizualizarePacientiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            VizualizarePacienti vizualizarePacienti = new VizualizarePacienti(limba, instance);
            vizualizarePacienti.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            vizualizarePacienti.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            vizualizarePacienti.Dock = DockStyle.Fill;
            control = vizualizarePacienti;
            this.Controls.Add(vizualizarePacienti);
        }

        private void examenOncologicToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareExamenOncologic adaugaExamenOncologic = new AdaugareExamenOncologic(limba, CNP);
            adaugaExamenOncologic.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaExamenOncologic.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaExamenOncologic.Dock = DockStyle.Fill;
            control = adaugaExamenOncologic;
            this.Controls.Add(adaugaExamenOncologic);
        }

        private void graficADSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareGraficADS adaugaGraficADS = new AdaugareGraficADS(limba, CNP);
            adaugaGraficADS.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaGraficADS.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaGraficADS.Dock = DockStyle.Fill;
            control = adaugaGraficADS;
            this.Controls.Add(adaugaGraficADS);
        }

        private void interventieChirurgicalaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareInterventieChirurgicala adaugaInterventieChirurgicala = new AdaugareInterventieChirurgicala(limba, CNP);
            adaugaInterventieChirurgicala.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaInterventieChirurgicala.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaInterventieChirurgicala.Dock = DockStyle.Fill;
            control = adaugaInterventieChirurgicala;
            this.Controls.Add(adaugaInterventieChirurgicala);
        }

        private void investigatiiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareInvestigatii adaugaInvestigatii = new AdaugareInvestigatii(limba, CNP);
            adaugaInvestigatii.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaInvestigatii.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaInvestigatii.Dock = DockStyle.Fill;
            control = adaugaInvestigatii;
            this.Controls.Add(adaugaInvestigatii);
        }

        private void medicatiiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareMedicatii adaugaMedicatii = new AdaugareMedicatii(limba, CNP);
            adaugaMedicatii.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaMedicatii.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaMedicatii.Dock = DockStyle.Fill;
            control = adaugaMedicatii;
            this.Controls.Add(adaugaMedicatii);
        }

        private void generarePDFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog pdfFile = new SaveFileDialog();
            pdfFile.InitialDirectory = "C:\\";
            pdfFile.Filter = "PDFs (*.pdf)|*.pdf";
            pdfFile.FilterIndex = 1;
            if (pdfFile.ShowDialog() == DialogResult.OK)
            {
                ExportPDF epdf = new ExportPDF(limba, pdfFile.FileName, CNP);

                XMLParser xmlPars = new XMLParser(@"C:\Language" + limba + ".xml");
                Dictionary<string, string> mesajeErori = xmlPars.ExtractAttributePairs("MesajeEroare", string.Empty, string.Empty, "name", "formula");

                try
                {
                    epdf.Generare();
                    MessageBox.Show(mesajeErori["pdfOk"], mesajeErori["TitluEroare"], MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch(Exception)
                {
                    MessageBox.Show(mesajeErori["pdfNOk"], mesajeErori["TitluInformation"], MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            resetGUI();
            VizualizareGrafic vizualizareGrafic = new VizualizareGrafic(CNP, limba);
            vizualizareGrafic.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            vizualizareGrafic.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            vizualizareGrafic.Dock = DockStyle.Fill;
            control = vizualizareGrafic;
            this.Controls.Add(vizualizareGrafic);
        }

        private void FormDBMedical_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Clean up AlertListener thread.
            alertListener.Stop();
        }

        private void vizualizareImaginiMedicaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            ImaginiMedicaleForm imaginiMedicale = new ImaginiMedicaleForm(CNP, limba);
            imaginiMedicale.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            imaginiMedicale.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            control = imaginiMedicale;
            this.Controls.Add(imaginiMedicale);
        }

        private void adaugaImaginiMedicaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareImaginiMedicale adaugaImgMed = new AdaugareImaginiMedicale(CNP, limba);
            adaugaImgMed.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaImgMed.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaImgMed.Dock = DockStyle.Fill;
            control = adaugaImgMed;
            this.Controls.Add(adaugaImgMed);
        }

        private void configurareToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            Configurare config = new Configurare(limba, instance);
            config.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            config.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            config.Dock = DockStyle.Fill;
            control = config;
            this.Controls.Add(config);
        }

        private void iesireToolStripMenuItem_Click(object sender, EventArgs e)
        {
            alertListener.Stop();
            this.Dispose();
        }

        private void igienaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareIgiena adaugaIgiena = new AdaugareIgiena(limba, CNP);
            adaugaIgiena.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaIgiena.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaIgiena.Dock = DockStyle.Fill;
            control = adaugaIgiena;
            this.Controls.Add(adaugaIgiena);
        }

        private void imbracatToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareImbracat adaugaImbracat = new AdaugareImbracat(limba, CNP);
            adaugaImbracat.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaImbracat.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaImbracat.Dock = DockStyle.Fill;
            control = adaugaImbracat;
            this.Controls.Add(adaugaImbracat);
        }

        private void utilitatiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            resetGUI();
            AdaugareUtilitati adaugaUtilitati = new AdaugareUtilitati(limba, CNP);
            adaugaUtilitati.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
            adaugaUtilitati.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            adaugaUtilitati.Dock = DockStyle.Fill;
            control = adaugaUtilitati;
            this.Controls.Add(adaugaUtilitati);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }

    }
}
